<?php
/**
 * Common data container, collection of maps or values
 * 
 * @package WKernel
 * 
 */
require_once 'WKernel/Data/Abstract.php';
class WK_Data_Collection extends WK_Data_Abstract {
	protected $data = array();
	
	public function __construct(&$data = null){
		if(!empty($data)) $this->data = &$data;
	}
	
	public function get($index){
		if(isset($this->data[$index]))
			return $this->castToObject($this->data[$index]);
		else
			return null;
	}
	
	public function getLength(){
		return count($this->data);
	}
	
	public function insert($value, $pos = null){
		$item = $this->dataToArray($value);
		if($pos !== null) {
			$end   = array_slice($this->data, $pos);
			$start = array_slice($this->data, 0, $pos - 1);
		}
		else{
			$start = $this->data;
			$end   = array();
		}
		if(!empty($end))
			array_unshift($end, $item);
		else 
			array_push($start, $item);
		$this->data = array_merge($start, $end);
	}
}